import { defineStore } from 'pinia';
import { cancelLike, insertLike, getLikeCount, hasLiked } from '../api/study/bizCourseVideoLikeController';

export const useAiMessageStore1 = defineStore('bizCourseVideoLike', {
  state: () => ({
    isLoading: false
  }),

  actions: {
    /**
     * 处理点赞逻辑
     * @param userId 用户ID
     * @param videoId 视频ID
     */
    async handleLikeAction(userId: number, videoId: number) {
      try {
        this.isLoading = true;
        const response = await insertLike({
          userId,
          videoId
        });
        console.log('点赞成功', response);
        return true;
      } catch (error) {
        console.error('点赞失败', error);
        return false;
      } finally {
        this.isLoading = false;
      }
    },

    /**
     * 处理取消点赞逻辑
     * @param userId 用户ID
     * @param videoId 视频ID
     */
    async handleCancelLikeAction(userId: number, videoId: number) {
      try {
        this.isLoading = true;
        const response = await cancelLike({
          userId,
          videoId
        });
        console.log('取消点赞成功', response);
        return true;
      } catch (error) {
        console.error('取消点赞失败', error);
        return false;
      } finally {
        this.isLoading = false;
      }
    },

    /**
     * 获取视频点赞数
     * @param userId 用户ID
     * @param videoId 视频ID
     * @returns 点赞数
     */
    async getVideoLikeCount(userId: number, videoId: number) {
      try {
        this.isLoading = true;
        const response = await getLikeCount({
          userId,
          videoId
        });
        console.log(`视频 ${videoId} 点赞数获取成功`, response);
        return response.data;
      } catch (error) {
        console.error(`视频 ${videoId} 点赞数获取失败`, error);
        throw error;
      } finally {
        this.isLoading = false;
      }
    },

    /**
     * 检查用户是否已点赞
     * @param userId 用户ID
     * @param videoId 视频ID
     * @returns 是否点赞
     */
    async checkHasLiked(userId: number, videoId: number) {
      try {
        this.isLoading = true;
        const response = await hasLiked({
          userId,
          videoId
        });
        console.log(`视频 ${videoId} 点赞状态获取成功`, response);
        return response.data;
      } catch (error) {
        console.error(`视频 ${videoId} 点赞状态获取失败`, error);
        throw error;
      } finally {
        this.isLoading = false;
      }
    }
  },
  
  getters: {
    
  }
});